package com.store.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.io.File;

/**
 * Web MVC 配置
 * 
 * @author Store Team
 */
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

    @Value("${file.upload.path:upload/images/}")
    private String uploadPath;

    /**
     * 配置静态资源映射
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // 获取项目根目录的绝对路径
        String projectPath = System.getProperty("user.dir");
        File uploadDir = new File(projectPath, uploadPath);
        
        // 确保上传目录存在
        if (!uploadDir.exists()) {
            uploadDir.mkdirs();
        }
        
        String absolutePath = uploadDir.getAbsolutePath() + File.separator;
        
        // 映射图片访问路径
        registry.addResourceHandler("/images/**")
                .addResourceLocations("file:" + absolutePath);
        
        System.out.println("项目路径: " + projectPath);
        System.out.println("图片路径映射: /images/** -> " + absolutePath);
    }
}






